package com.winning.ds.login.controller;

import com.winning.ds.common.controller.BaseController;
import com.winning.ds.common.entity.SysGncdEntity;
import com.winning.ds.common.util.ResponseEntity;
import com.winning.ds.login.service.DbsxService;
import com.winning.ds.login.service.IndexService;
import com.winning.ds.login.entity.SysDbsxbEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName NotifyMsgController
 * @Description TODO
 * @Author WJX
 * @Date 2020/4/8 15:02
 **/
@Slf4j
@RestController
@RequestMapping("/dbsxMsg")
public class DbsxController extends BaseController {

    @Autowired
    private IndexService indexService;

    @Autowired
    private DbsxService dbsxService;

    /**
     * 查询待办事项表
     * @return
     */
    @GetMapping(value = "/getNoHandleCount")
    public ResponseEntity dbsxList() {
        ArrayList<String> cdbmList = new ArrayList<>();
        //查询当前用户有的菜单权限
        List<SysGncdEntity> cdList = indexService.queryNavigationMenu(indexService.getJsbmList());
        List<SysGncdEntity> thirdList = new ArrayList<>();
        for(SysGncdEntity cdEntity : cdList){
            String mcode = cdEntity.getMcode();
            Map<String, Object> menuMap = indexService.querySubMenu(mcode);
            List<SysGncdEntity> secondList = (List)menuMap.get("secondMenu");
            thirdList.addAll(secondList);
        }

        if(thirdList.isEmpty()){
            return ResponseEntity.ok();
        }
        for(SysGncdEntity thirdEntity : thirdList){
            String thirdCdbmcode = thirdEntity.getMcode();
            int cdjb = thirdEntity.getCdjb();
            if(2 == cdjb){
                cdbmList.add(thirdCdbmcode);
            }
        }
        if(cdbmList.isEmpty()){
            return ResponseEntity.ok();
        }
        //查询待办事项
        List<SysDbsxbEntity> dbsxList= dbsxService.getDbsxList(cdbmList);
        //如果待办事项为空，说明没有权限
        if(null == dbsxList || dbsxList.isEmpty()){
            return ResponseEntity.ok();
        }

        List<Map<String, String>> list = new ArrayList<>();
        for(SysDbsxbEntity dbsxb : dbsxList){
            String dbSql = dbsxb.getDbsql();
            if(StringUtils.isNotBlank(dbSql)){
                Map<String, Object> map = dbsxService.getDbsxData(dbSql);
                list.add(dbsxService.formatResult(dbsxb,map));
            }
        }

        return ResponseEntity.ok().put("dbsxlist",list);
    }

}
